<?php
/* 
* @Author: LDF QQ 47121862
* @Date:   2014-06-07 10:50:00
* @Last Modified by:   LDF QQ 47121862
* @Last Modified time: 2014-06-11 18:00:23
*/
class XilieModel extends Model{
	public $table='xilie';
	public $validate=array(
		array('title','nonull','系列名称不能为空',2,3)
	);
	public $auto=array(
		array('pic','pic','method',2,3)
	);
	//系列图片处理 图片大小 封面 210*280 幻灯片900*350
	public function pic(){
		if(!empty($_FILES['pic']['name'])){
			if(isset($_POST['oldpic'])) @unlink(ROOT_PATH.$_POST['oldpic']); //删除原有图片
			if(isset($_POST['oldslidepic'])) @unlink(ROOT_PATH.$_POST['oldslidepic']); //删除原有图片
			$u = new Upload();
			$i = new Image();
			$info = $u->upload('pic');
			//如果是幻灯片
			if(isset($_POST['slide'])){
				$big = $info[0]['dir'].$info[0]['filename'].'_900x350.'.$info[0]['ext'];
				$i->thumb($info[0]['path'],$big,900,350);
				$this->data['slidePic']=$big;
				if(isset($_POST['oldslidepic'])) $_POST['slidePic']=$big;
			}
			$pic = $info[0]['dir'].$info[0]['filename'].'_210x280.'.$info[0]['ext'];
			$i->thumb($info[0]['path'],$pic,210,280);
			//删除上传的源文件
			@unlink(ROOT_PATH.$info[0]['path']);
			return $pic;
		}
	}
	//添加系列
	function addXilie(){
		if($this->create()){
			$this->data['uid']=$_SESSION['user']['uid'];
			if($xid = $this->add()){
				$juji=array( //组合剧集需要数据，同时插入第一集
					'xid'=>$xid,
					'intro'=>$_POST['intros'],
					'daoyan'=>$_POST['daoyan'],
					'zhuyan'=>$_POST['zhuyan'],
					'url'=>$_POST['firstUrl']
				);
				M('juji')->add($juji);
				return $xid;
			}else{
				$this->error='插入系列失败';
				return 0;
			}
		}else{
			$this->error='添加系列失败';
			return 0;
		}
	}
	//修改系列
	function editXilie(){
		if(!empty($_FILES['pic']['name'])){
			if($pic=$this->pic()) $_POST['pic']=$pic;
		}
		return $this->save($_POST)?1:0;
	}
	//删除系列
	function delXilie(){
		$xid=Q('xid',0,'intval');
		$xilie=$this->all($xid);
		foreach ($xilie as $k => $v) { //删除相关图片
			if($v['pic']) @unlink(ROOT_PATH.$v['pic']);
			if($v['slidePic']) @unlink(ROOT_PATH.$v['slidePic']);
		}
		//删除系列
		if($this->del($xid)){ 
			//删除相关剧集
			M('juji')->where('xid='.$xid)->del();
			//删除相关评论
			M('xcomment')->where('xid='.$xid)->del();
			return 1;
		}else{
			return 0;
		}
	}
}
